<?php
apf_require_class('APF_Controller');
apf_require_class('Util_JxUrls');
apf_require_class('Const_Tip');
apf_require_class('Const_JxStatus');
apf_require_class('Bll_Jx_Relation');
//apf_require_class('Bll_Hr_OutInterface');
abstract class AbstractController extends APF_Controller {

    /**
     * @var APF
     */
    protected $apf = NULL;

    /**
     * @var NHRequest
     */
    protected $request = NULL;

    /**
     * @var APF_Response
     */
    protected $response = NULL;

    /**
     * @ 流程列表
     */
    protected $flow_list = NULL;

    //用户信息
    protected $user_info;

    public function __construct() {
        parent::__construct();
        $this->apf = APF::get_instance();
        $this->request = $this->apf->get_request();
        $this->response = $this->apf->get_response();
        //$this->oahr = new Bll_Hr_OutInterface();
        $this->is_logout();
    }

    /**
     * 退出登录
     */
    public function is_logout(){
        //TODO 销毁用户信息 跳转到登录页面
        if(isset($_GET['logout'])){
            $cookie_name = $this->apf->get_config('AuthCookieName');
            $domain = $this->apf->get_config('cookie_domain');
            $path = $this->apf->get_config('cookie_path');
            setcookie($cookie_name, $cookie_str, time() - 86400, $path, $domain, false, false);

            $oauth = APF::get_instance()->get_config('oauth_info');
            if($oauth){
                $logout = $oauth['oauth_url'].'/logout.php?client_id='.$oauth['client_id'].'&client_secret='.$oauth['client_secret'];
                $this->apf->get_response()->redirect($logout,false,false);
            }else{
                $this->apf->get_response()->redirect('/login/',false,false);
            }
        }
    }
    /**
     *
     * @param int $status 0错误 1成功
     * @param unknown_type $res
     */
    public function json_encode($status,$res){
        $result = array(
                'errcode'=>$status,
                'data'=>$res
        );
        echo json_encode($result);
        exit();
    }
    /**
     * 跳转到提示页面
     * @param int $status 状态，1：正确状态，0：错误状态
     * @param str $code 错误码，统一到tip.php内定义
     * @param str $next 跳转到哪个链接
     */
    protected function show_tips($code = 'ERROR_SYSTEM', $next='', $status = 0){
        $next = !empty($next)
        ? $next
        : ((!empty($_SERVER['HTTP_REFERER']) && !strpos($_SERVER['HTTP_REFERER'], 'ShowTips')) ? $_SERVER['HTTP_REFERER'] : Util_JxUrls::base_url());

        $params = "?status=$status&code=$code";

        if($next){
            $params .= '&next='.base64_encode($next);
        }
        $url = Util_JxUrls::build_jump_url($params);
        $this->response->redirect($url);
    }
}
